package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.IotProductYx;
import com.econ.powercloud.ems.dal.mapper.IotProductYxMapper;
import com.google.common.collect.Lists;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description : iot_product_yx 数据源层
 * @Date : 2021/6/29 13:55
 * @Modified by : zjf
 */
@Repository
public class IotProductYxDAO extends ServiceImpl<IotProductYxMapper, IotProductYx> {

    /**
     * 通过productKey与moduleCode来查询遥测数据类型有哪些
     */
    public List<Integer> selectStatusTypeByProductKeyAndModuleCode(String productKey, String moduleCode){
        LambdaQueryWrapper<IotProductYx> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYx::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYx::getModuleCode,moduleCode);
        List<IotProductYx> ycSyncList = this.baseMapper.selectList(lambdaQueryWrapper);
        if(CollectionUtils.isEmpty(ycSyncList)){
            return Lists.newArrayList();
        }
        return Lists.newArrayList(ycSyncList.stream().collect(Collectors.groupingBy(d->d.getStatusTypeCode())).keySet());
    }

    /**
     * 根据productKey,moduleCode,点号名称,遥测数据类型分页查找
     */
    public Page<IotProductYx> selectPageByCondition(String productKey, String moduleCode, Integer statusType, String remark,
                                                        Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<IotProductYx> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYx::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYx::getModuleCode,moduleCode);
        lambdaQueryWrapper.eq(statusType != null, IotProductYx::getStatusTypeCode,statusType);
        lambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(remark), IotProductYx::getName,remark);
        Page<IotProductYx> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,lambdaQueryWrapper);
    }

    public IotProductYx selectYxByDit(String dit, String productKey) {
        LambdaQueryWrapper<IotProductYx> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StringUtils.isNullOrEmpty(productKey),IotProductYx::getProductKey,productKey);
        wrapper.eq(IotProductYx:: getDit,dit);
        wrapper.last("limit 1");
        return this.baseMapper.selectOne(wrapper);
    }

    public List<IotProductYx> selectListByProductKeysAndDits(List<String> productKeys,List<String> dits){
        LambdaQueryWrapper<IotProductYx> wrapper = Wrappers.lambdaQuery();
        wrapper.in(IotProductYx::getProductKey,productKeys);
        wrapper.in(IotProductYx::getDit,dits);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * @param search : 点号名称或标识
     * @param statusTypeCode:
     * @return
     */
    public Page<IotProductYx> selectPageBySearch(String productKey, String moduleCode, Integer statusTypeCode,  String search, Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<IotProductYx> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(IotProductYx::getProductKey,productKey);
        lambdaQuery.eq(IotProductYx::getModuleCode,moduleCode);
        lambdaQuery.eq(statusTypeCode != null,IotProductYx::getStatusTypeCode,statusTypeCode);
        lambdaQuery.and(!StringUtils.isNullOrEmpty(search),wrapper -> wrapper.like(IotProductYx::getDit, search).or().like(IotProductYx::getName, search));
        Page<IotProductYx> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,lambdaQuery);
    }

    public List<IotProductYx> selectByProductKeyAndModuleCode(String productKey, String moduleCode,List<Integer> statusTypeCodes){
        LambdaQueryWrapper<IotProductYx> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(IotProductYx::getProductKey,productKey);
        lambdaQueryWrapper.eq(IotProductYx::getModuleCode,moduleCode);
        lambdaQueryWrapper.in(IotProductYx::getStatusTypeCode,statusTypeCodes);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }
}
